package cn.com.mall.product.service.impl;

import cn.com.mall.product.dao.BrandDao;
import cn.com.mall.product.dao.CategoryDao;
import cn.com.mall.product.entity.BrandEntity;
import cn.com.mall.product.entity.CategoryEntity;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.mall.common.utils.PageUtils;
import cn.com.mall.common.utils.Query;

import cn.com.mall.product.dao.CategoryBrandRelationDao;
import cn.com.mall.product.entity.CategoryBrandRelationEntity;
import cn.com.mall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);


    }

    @Override
    public List<CategoryBrandRelationEntity> getCatelogListByBrandId(Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> brand_id = new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId);

        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.getBaseMapper().selectList(brand_id);
        return categoryBrandRelationEntities;
    }

    @Override
    public List<CategoryBrandRelationEntity> getBrandListByCatelogId(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> brand_id = new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId);

        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.getBaseMapper().selectList(brand_id);
        return categoryBrandRelationEntities;
    }

    @Override
    public void updateBCategoryBrandNameByBrandId(Long brandId, String brandName) {
        CategoryBrandRelationEntity categoryBrandRelationEntity=new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(brandName);
        categoryBrandRelationEntity.setBrandId(brandId);

        UpdateWrapper<CategoryBrandRelationEntity> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("brand_id",brandId);

        this.getBaseMapper().update(categoryBrandRelationEntity,updateWrapper);

    }


}